package com.lyc.common.holder;

import java.util.Optional;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.lyc.common.entity.UserInfo;

/**
 * 当前登录用户信息
 * @Author 李大神仙
 * @Date 2024/3/2 13:30
 * @Version 1.0
 */
public class UserContextHolder {
    private final TransmittableThreadLocal<UserInfo> threadLocal = new TransmittableThreadLocal<>();

    public UserContextHolder() {
    }

    public static UserContextHolder getInstance(){
        return SingletonHolder.INSTANCE;
    }

    public void setContext(UserInfo userInfo){
        this.threadLocal.set(userInfo);
    }

    public UserInfo getContext(){
        return this.threadLocal.get();
    }

    public String getAccount(){
        return (Optional.ofNullable(this.threadLocal.get()).orElse(new UserInfo())).getAccount();
    }

    public boolean isAdmin(){
        return (Optional.ofNullable(this.threadLocal.get()).orElse(new UserInfo())).isAdmin();
    }

    public String getAuthorization(){
        return (Optional.ofNullable(this.threadLocal.get()).orElse(new UserInfo())).getAuthorization();
    }

    public String getAuthAccount(){
        return (Optional.ofNullable(this.threadLocal.get()).orElse(new UserInfo())).getAccount();
    }

    public String getUserName(){
        return (Optional.ofNullable(this.threadLocal.get()).orElse(new UserInfo())).getUsername();
    }

    public String getAppCode(){
        return (Optional.ofNullable(this.threadLocal.get()).orElse(new UserInfo())).getAppCode();
    }

    public String getLanguage(){
        return (Optional.ofNullable(this.threadLocal.get()).orElse(new UserInfo())).getLanguage();
    }

    public String getOrgId(){
        return (Optional.ofNullable(this.threadLocal.get()).orElse(new UserInfo())).getOrgId();
    }

    public Long getUserId(){
        return (Optional.ofNullable(this.threadLocal.get()).orElse(new UserInfo())).getUserId();
    }

    public String getTenantId(){
        return (Optional.ofNullable(this.threadLocal.get()).orElse(new UserInfo())).getTenantId();
    }

    public String getApplicationOrganize(){
        return (Optional.ofNullable(this.threadLocal.get()).orElse(new UserInfo())).getApplicationOrganize();
    }

    public String getApplicationOrganizeId(){
        return (Optional.ofNullable(this.threadLocal.get()).orElse(new UserInfo())).getApplicationOrganizeId();
    }

    public String getIp(){
        return (Optional.ofNullable(this.threadLocal.get()).orElse(new UserInfo())).getIp();
    }

    public void clear(){
        this.threadLocal.remove();
    }

    private static class SingletonHolder{
        private static final UserContextHolder INSTANCE = new UserContextHolder();
        private SingletonHolder(){
        }
    }

}

































